package cn.cjxy.session01;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/get")
public class SetSessionDemo extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(req.getSession().getId());
        String country = (String) req.getSession().getAttribute("country");
        //创建cookie并存储会话id信息
        Cookie jsessionid = new Cookie("JSESSIONID", req.getSession().getId());
        //设置过期时间10分钟
        jsessionid.setMaxAge(60*10);
        //添加cookie
        resp.addCookie(jsessionid);
        //取值并输出
        resp.getWriter().print(country);
    }
}
